package com.zy.ioc.pro5_ioc_import;

import com.zy.ioc.pro5_ioc_import.other.Apple;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Random;

/**
 * @program: mavenTest153
 * @description: 用于扩展bean的导入功能
 * @author: zy
 * @create: 2025-07-13 08:58
 */
public class MyImportBeanDefinitionRegistar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //1.spring的容器中是否有Apple这个bean
       boolean b=registry.containsBeanDefinition(   Apple.class.getName());
       // "apple"
        Random r=new Random();
       String beanId=Apple.class.getSimpleName().substring(0,1).toLowerCase()+Apple.class.getSimpleName().substring(1)+"_"+r.nextInt(10000);
       if( b==false){
           //没有这个bean，就注册一个bean
           RootBeanDefinition rb=new RootBeanDefinition(Apple.class);

           registry.registerBeanDefinition(beanId,rb);
       }

    }
}
